package com.guo.worktool.manager.factory;

import com.guo.worktool.common.comps.RedisUtil;
import com.guo.worktool.utils.ServletUtils;
import com.guo.worktool.utils.ip.AddressUtils;
import com.guo.worktool.utils.ip.IpUtils;
//import eu.bitwalker.useragentutils.UserAgent;
import lombok.extern.slf4j.Slf4j;

import java.util.TimerTask;

/**
 * 异步工厂（产生任务用）
 *
 */
@Slf4j
public class AsyncFactory {
    private static final String visitsKey = "visits:rank:daily";



    /**
     * 记录登录信息
     */
//    public static TimerTask recordLogininfor(RedisUtil redisUtil, final Object... args) {
//        //获取客户访问ip
//        final String ipaddr = IpUtils.getIpAddr(ServletUtils.getRequest());
//        redisUtil.increment(ipaddr, 1);
//        return new TimerTask() {
//            final UserAgent userAgent = UserAgent.parseUserAgentString(ServletUtils.getRequest().getHeader("User-Agent"));
//            @Override
//            public void run() {
//                //获取 客户访问地址
//                String loginLocation = AddressUtils.getRealAddressByIP(ipaddr);
//                // 获取客户端操作系统
//                String os = userAgent.getOperatingSystem().getName();
//                // 获取客户端浏览器
//                String browser = userAgent.getBrowser().getName();
//                log.info("登录地址:{},登录地点:{}, 操作系统:{}, 端浏览器:{}", ipaddr, loginLocation, os, browser);
////                VisitorRecords visitorRecords = new VisitorRecords(ipaddr, loginLocation, browser, os);
////                log.info("visitorRecords:{}", JSON.toJSONString(visitorRecords));
//            }
//        };
//    }


    public static void getVisitorRecords(RedisUtil redisUtil, String ipaddr){
        try {
            redisUtil.addScoreByIpMethod(ipaddr, 1, visitsKey);
            //获取 客户访问地址
            String loginLocation = AddressUtils.getRealAddressByIP(ipaddr);
            log.info("登录地址:{},登录地点:{}", ipaddr, loginLocation);
        }catch (Exception e){
            log.error("getVisitorRecordsException:{}", e.getMessage());
        }
    }

}
